<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 * 矿机管理
 */
namespace app\product\model;

use app\product\model\Model;
use Mscore\Core\Lib\Upload;

class Protrade extends Model{
	
    private $tradeinfo = 'pro_tradeinfo';

    /**
     * 列表
     * @param array $list 
     * @param array $where 
     */ 
    public function list($where=null,$page=1,$page_size=10){
        //矿机列表
        $list['datalist']= Model::name($this->tradeinfo)
            ->alias('a')
            ->field('a.*')
            ->where($where)
            ->limit(($page-1)*$page_size,$page_size)
            ->order('a.orderid asc, a.id desc')
            ->select();
        //数据总数
        $list['total']=Model::name($this->tradeinfo)
            ->alias('a')
            ->where($where)
            ->count('id');
        return $list;
    }

    /**
     * @param array $info 
     * @param array $where 
     */ 
    public function info($id=null,$field='*'){  
        $info=Model::name($this->tradeinfo)
           ->field($field)
           ->where(['id'=>$id])
           ->find();
        if(isset($info['sentiment'])){
            $info['sentiment']=json_decode($info['sentiment'],true);  
        }
        if(isset($info['contract'])){
            $info['contract']=json_decode($info['contract'],true);  
        }
        if(isset($info['forecast'])){
            $info['forecast']=json_decode($info['forecast'],true);  
        }
        return  $info;
    }

    /**
     * 保存
     * @param array $save 
     * @param array $where 
     */ 
    public function save($data=[]){
        $id=isset($data['id'])&&!empty($data['id'])?$data['id']:null;
        unset($data['id']);
        $data  = [
            'title'=>$data['title'],
            'proid'=>$data['proid'],
            'procode'=>$data['procode'],
            'cid'=>$data['cid'],
            'cname'=>$data['cname'],
            'sentiment'=>json_encode($data['sentiment'],JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE),
            'contract'=>json_encode($data['contract'],JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE),
            'forecast'=>json_encode($data['forecast'],JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE),
            'status'=>$data['status'],
            'createtime'=>date('Y-m-d H:i:s',time())
        ];
        switch (true) {
            case !empty($id):
                Model::name($this->tradeinfo)->where('id='.$id)->update($data);
                return  $id;
                break;
            default:
                $id = Model::name($this->tradeinfo)->insertGetId($data);
                return $id;
                break;
        }
        return false;
    }

}
